package com.xhwl.data.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.common.dto.cent.sdata.EnterpriseConfigDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.cent.sdata.EnterpriseConfig;
import com.xhwl.common.query.cent.sdata.EnterpriseQuery;
import com.xhwl.common.utils.StringUtils;
import com.xhwl.common.vo.cent.sdata.EnterpriseSessionTimeVO;
import com.xhwl.data.service.IEnterpriseConfigService;
import com.xhwl.data.service.IEnterpriseService;
import com.xhwl.data.service.IThemeConfigService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.xpath.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 企业个性化配置
 *
 * @Author: lrp
 * @Description: 企业个性化配置
 * @Date: Create in 9:05 2021/10/11
 * @menu 企业个性化配置
 */
@RestController
public class EnterpriseConfigController {
    @Autowired
    private IEnterpriseConfigService enterpriseConfigService;

    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private IThemeConfigService themeConfigService;

    /**
     * 分页
     *
     * @param enterpriseName
     * @param account
     * @param page
     * @return
     */
    @RequestMapping(value = "/enterprise-config/page")
    public ResultJson page(@RequestParam(value = "enterpriseName", required = false) String enterpriseName,
                           @RequestParam(value = "idList", required = false) List<Integer> idList,
                           Account account,
                           Page page) {
        page.setRecords(OrderItem.descs("created_at"));
        EnterpriseQuery enterpriseQuery = new EnterpriseQuery();
        if (!StringUtils.isEmpty(enterpriseName)) {
            enterpriseQuery.setName(enterpriseName);
        }
        if (CollectionUtils.isNotEmpty(idList)) {
            enterpriseQuery.setIdList(idList);
        }
        enterpriseQuery.setShowChild(true);
        Page<EnterpriseConfig> list = enterpriseConfigService.page(enterpriseQuery, account, page);
        return ResultJson.success(list);
    }


    /**
     * 更新
     *
     * @param enterpriseConfigDTO
     * @return
     */
    @PostMapping(value = "/enterprise-config/update")
    public ResultJson update(@RequestHeader(value = "industryId") Short industryId,
                             @RequestBody EnterpriseConfigDTO enterpriseConfigDTO) {
        ResultJson checkResult = check(enterpriseConfigDTO);
        if (!checkResult.getState()) {
            return checkResult;
        }
        // 非必填
        if (1 == enterpriseConfigDTO.getOpenStatus() && enterpriseConfigDTO.getPermissionId().length() == 0) {
            return ResultJson.fail("增加水印权限为空");
        }
        // 非必填
        if (CollectionUtils.isNotEmpty(enterpriseConfigDTO.getAppConfigList())) {
            if (enterpriseConfigDTO.getAppConfigList().size() > 5) {
                return ResultJson.fail("参数错误，移动端产品配置在1~5个范围内");
            }
        }
        enterpriseConfigDTO.setIndustryId(industryId);
        Boolean flag = enterpriseConfigService.entConfigExistCheck(enterpriseConfigDTO);
        if (flag) {
            Boolean linkCodeEntFlag = enterpriseConfigService.linkCodeAndEntExistCheck(enterpriseConfigDTO);
            if (!linkCodeEntFlag) {
                return ResultJson.fail("访问地址已存在");
            }
            if (null != enterpriseConfigDTO.getId()) {
                Boolean linkCodeFlag = enterpriseConfigService.linkCodeExistCheck(enterpriseConfigDTO);
                if (!linkCodeFlag) {
                    return ResultJson.fail("访问地址已存在");
                }
            } else {
                Boolean entFlag = enterpriseConfigService.entExistCheck(enterpriseConfigDTO);
                if (!entFlag) {
                    return ResultJson.fail("企业配置信息已存在");
                }
            }
        } else {
            if (null == enterpriseConfigDTO.getId()) {
                return ResultJson.fail("企业配置信息已存在");
            }
        }
        enterpriseConfigService.update(industryId, enterpriseConfigDTO);
        return ResultJson.success();
    }


    /**
     * 获取企业个性化配置
     *
     * @header industryId
     * @param enterpriseId
     * @param linkCode
     * @return
     */
    @RequestMapping(value = "/enterprise-config/get-ent-conf")
    public ResultJson getEntConf(@RequestParam(value = "industryId", required = false) Short industryId,
                                 @RequestParam(value = "enterpriseId", required = false) Integer enterpriseId,
                                 @RequestParam(value = "linkCode", required = false) String linkCode) {
        return enterpriseConfigService.findEntConf(industryId, enterpriseId, linkCode);
    }


    /**
     * 设置企业会话超时时间
     *
     * @param enterpriseConfigDTO
     * @return
     */
    @PostMapping(value = "/enterprise-config/session-time/set")
    public ResultJson sessionTimeSet(@RequestBody EnterpriseConfigDTO enterpriseConfigDTO) {
        return enterpriseConfigService.setSessionTime(enterpriseConfigDTO);
    }


    /**
     * 获取企业会话超时时间
     *
     * @param enterpriseId
     * @return
     */
    @GetMapping(value = "/enterprise-config/session-time/get")
    public ResultJson sessionTimeGet(@RequestParam(value = "enterpriseId") Integer enterpriseId) {
        EnterpriseConfig sessionTime = enterpriseConfigService.getSessionTime(enterpriseId);
        EnterpriseSessionTimeVO resultVo = new EnterpriseSessionTimeVO();
        if (null != sessionTime) {
            resultVo.setEnterpriseId(sessionTime.getEnterpriseId());
            resultVo.setLimitTimeUnit(sessionTime.getLimitTimeUnit());
            resultVo.setSessionLimitTime(sessionTime.getSessionLimitTime());
        }
        return ResultJson.success(resultVo);
    }


    private ResultJson check(EnterpriseConfigDTO enterpriseConfigDTO) {
        if (null == enterpriseConfigDTO) {
            return ResultJson.fail("参数错误");
        }
        if (null == enterpriseConfigDTO.getEnterpriseId()) {
            return ResultJson.fail("缺少enterpriseId");
        }
        if (null == enterpriseConfigDTO.getLinkCode()) {
            return ResultJson.fail("缺少linkCode参数");
        }
        if (null == enterpriseConfigDTO.getLogoUrl()) {
            return ResultJson.fail("缺少logo参数");
        }
        if (null == enterpriseConfigDTO.getName()) {
            return ResultJson.fail("缺少name参数");
        }
        if (null == enterpriseConfigDTO.getIcoUrl()) {
            return ResultJson.fail("缺少icoUrl参数");
        }
        if (null == enterpriseConfigDTO.getBackgroundUrl()) {
            return ResultJson.fail("缺少backgroundUrl参数");
        }
        if (null == enterpriseConfigDTO.getLoginWord()) {
            return ResultJson.fail("缺少loginWord参数");
        }
        if (null == enterpriseConfigDTO.getThemeColor()) {
            return ResultJson.fail("缺少themecolor参数");
        }
        // 样式为4时，需配置轮播图
        if (enterpriseConfigDTO.getPlatformStyle() == 3 && null == enterpriseConfigDTO.getSlideUrls() && enterpriseConfigDTO.getSlideUrls().trim().length() == 0) {
            return ResultJson.fail("轮播图配置为空");
        }
        return ResultJson.success("检查通过");
    }
}
